package middleware

import (
	"blog_server/global"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"time"
)

func AccessLog() gin.HandlerFunc {
	return func(c *gin.Context) {
		beginTime := time.Now().Format("2006-01-02 15:04:05")
		c.Next()
		endTime := time.Now().Format("2006-01-02 15:04:05")
		entry := global.Logger.WithFields(logrus.Fields{
			"method":    c.Request.Method,
			"path":      c.Request.URL.Path,
			"IP":        c.Request.Header.Get("X-Forward-For"),
			"dataSize":  c.Writer.Size(),
			"beginTime": beginTime,
			"endTime":   endTime,
			"status":    c.Writer.Status(),
			"agent":     c.Request.UserAgent(),
		})
		entry.Info("AccessLog")
	}
}
